package com.u79w.amap;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.AMapLocationClientOption;

public class AMapLocationPlugin extends CordovaPlugin implements AMapLocationListener{

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明mLocationOption对象
    public AMapLocationClientOption mLocationOption = null;

    CallbackContext currentCallbackContext;

    @Override
    public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
        // save the current callback context
        //Log.d("AMapLocationPlugin","AMapLocationPlugin,execute");
        if (action.equals("getAmapLocation")) {
            //单次
            currentCallbackContext = callbackContext;
            getAmapLocation();
        }
        return true;
    }

    public void getAmapLocation() {
        //启动定位
        mLocationClient.startLocation();
    }

    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        //Log.d("AMapLocationPlugin","AMapLocationPlugin,initialize");
        super.initialize(cordova, webView);
        //初始化定位
        mLocationClient = new AMapLocationClient(this.cordova.getActivity().getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(this);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(true);
        //设置是否强制刷新WIFI，默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(0);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //Log.d("AMapLocationPlugin","AMapLocationPlugin,initialize====end");
    }

    public void onLocationChanged(AMapLocation amapLocation) {
        JSONObject result = new JSONObject();
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {
                //定位成功回调信息，设置相关消息
                result.put("errCode", 0);
                result.put("adcode",amapLocation.getAdCode());
                result.put("lng",amapLocation.getLongitude());
                result.put("lat",amapLocation.getLatitude());
                result.put("name",amapLocation.getAddress());
                result.put("address",amapLocation.getAddress());
                result.put("address2",amapLocation.getStreet()+amapLocation.getStreetNum());
                
                if(currentCallbackContext != null) {
                    currentCallbackContext.success(result);
                }
            } else {
                //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                result.put("errCode", amapLocation.getErrorCode());
                result.put("errInfo", amapLocation.getErrorInfo());
                if(currentCallbackContext != null) {
                    currentCallbackContext.error(result);
                }
            }
        }else {
            result.put("errCode", "-1");
            result.put("errInfo", "amapLocation undefined!");
            if(currentCallbackContext != null) {
                currentCallbackContext.error(result);
            }
        }
    }
}